package com.company.learnjpa.view;

import com.company.learnjpa.model.entity.Blog;
import com.company.learnjpa.model.entity.Comment;
import com.company.learnjpa.model.repository.BlogRepository;
import com.company.learnjpa.util.Helper;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validator;
import java.io.Serializable;
import java.util.Set;
import org.primefaces.PrimeFaces;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DialogFrameworkOptions;

/**
 *
 * @author xiaolei
 */
@Named(value = "blogDetailView")
@ViewScoped
public class BlogDetailView implements Serializable {

    private Long blogId;
    private Blog blog;
    private String newComment;

    @Inject
    private BlogRepository blogRepository;

    @Inject
    private Validator validator;

    public BlogDetailView() {
    }

    public void init() {
        if (blogId != null) {
            blog = blogRepository.findById(blogId);
            if (blog == null) {
                Helper.redirect("/home.xhtml");
            }
        } else {
            Helper.redirect("/home.xhtml");
        }

    }

    public void addComment() {
        Comment comment = new Comment();
        comment.setBlog(blog);
        comment.setComment(newComment);

        // 验证输入数据
        Set<ConstraintViolation<Comment>> violations = validator.validate(comment);
        if (!violations.isEmpty()) {
            for (ConstraintViolation<Comment> violation : violations) {
                Helper.addErrorMessage("Error", violation.getMessage());
            }
            return;
        }

        blog.getCommentList().add(comment); // 添加评论到Blog
        if (blogRepository.update(blog) == null) {
            Helper.addErrorMessage("Error", "评论失败");
        } else {
            newComment = "";
        }
    }

    public void openBlogEditor(Blog blog) {
        DialogFrameworkOptions options = DialogFrameworkOptions.builder()
                .resizable(false)
                .draggable(false)
                .maximizable(true)
                .responsive(true)
                .width("900px")
                .contentWidth("100%")
                .modal(true)
                .fitViewport(true)
                .build();

        Helper.addFlashObject("blog", blog);

        PrimeFaces.current().dialog().openDynamic("edit-blog", options, null);
    }

    public void handleDialogReturn(SelectEvent<Blog> event) {
        blog = event.getObject();
    }

    public Long getBlogId() {
        return blogId;
    }

    public void setBlogId(Long blogId) {
        this.blogId = blogId;
    }

    public Blog getBlog() {
        return blog;
    }

    public void setBlog(Blog blog) {
        this.blog = blog;
    }

    public String getNewComment() {
        return newComment;
    }

    public void setNewComment(String newComment) {
        this.newComment = newComment;
    }

}
